import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class D2 {
    //线程共享
    //private static List<D2Poll> d2Polls = new ArrayList<D2Poll>();

    static Lock lock = new ReentrantLock();

    //线程独占
    private static ThreadLocal<ConnectionT> localConnectionT = new ThreadLocal<ConnectionT>();

    //线程独占
    private static ThreadLocal<SpringMangerT> localSpringMangerT = new ThreadLocal<SpringMangerT>();


    public static void main(String[] args) {

        new Thread(new MyTask01("线程MyTask01-1"), "线程MyTask01-1").start();
        new Thread(new MyTask01("线程MyTask01-2"), "线程MyTask01-2").start();


        //new Thread(new MyTask02("线程MyTask02-1"),"线程MyTask02-1").start();
        //new Thread(new MyTask02("线程MyTask02-2"),"线程MyTask02-2").start();
        //new Thread(new MyTask("线程3")).start();
        //new Thread(new MyTask("线程4")).start();
        //new Thread(new MyTask("线程5")).start();


    }


    public static class MyTask01 implements Runnable {
        private String name;

        MyTask01(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            //lock.lock();
            for (int i = 0; i < 5; i++) {
                /*d2Polls.add(new D2Poll());
                for (D2Poll d2Poll : d2Polls) {
                    System.out.println("线程" + name + ": " + d2Poll);
                }*/


                // ThreadLocal.get方法获取线程变量
                if (null == D2.localConnectionT.get()) {
                    // ThreadLocal.et方法设置线程变量
                    D2.localConnectionT.set(new ConnectionT("ConnectionT["+ name  +"]->"));
                    //System.out.println("线程" + name + ": 0");
                } else {
                    System.out.println("线程" + name + "->获取容器ConnectionT对象: " + D2.localConnectionT.get());
                    /*if (i == 3) {
                        D2.localConnectionT.remove();
                    }*/
                }

                // ThreadLocal.get方法获取线程变量
                if (null == D2.localSpringMangerT.get()) {
                    // ThreadLocal.et方法设置线程变量
                    D2.localSpringMangerT.set(new SpringMangerT("SpringMangerT["+ name  +"]->"));
                    //System.out.println("线程" + name + ": 0");
                } else {
                    System.out.println("线程" + name + "->获取容器SpringMangerT对象: " + D2.localSpringMangerT.get());
                    /*if (i == 3) {
                        D2.localSpringMangerT.remove();
                    }*/
                }


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //lock.unlock();
        }

    }


    public static class MyTask02 implements Runnable {
        private String name;

        MyTask02(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            //lock.lock();
            for (int i = 0; i < 5; i++) {
                /*d2Polls.add(new D2Poll());
                for (D2Poll d2Poll : d2Polls) {
                    System.out.println("线程" + name + ": " + d2Poll);
                }*/


                // ThreadLocal.get方法获取线程变量
                if (null == D2.localSpringMangerT.get()) {
                    // ThreadLocal.et方法设置线程变量
                    D2.localSpringMangerT.set(new SpringMangerT("SpringMangerT["+ name  +"]->"));
                    //System.out.println("线程" + name + ": 0");
                } else {
                    System.out.println("线程" + name + "->获取容器SpringMangerT对象: " + D2.localSpringMangerT.get());
                    /*if (i == 3) {
                        D2.localSpringMangerT.remove();
                    }*/
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //lock.unlock();
        }

    }

}


class ConnectionT {
    public ConnectionT(String name) {
        System.out.println(name + "ConnectionT初始化");
    }
}

class SpringMangerT {
    public SpringMangerT(String name) {
        System.out.println(name + "SpringMangerT初始化");
    }
}

class D2Poll {
    public D2Poll() {
        System.out.println("D2Poll初始化");
    }

    @Override
    public String toString() {
        return "D2Poll{}";
    }
}
